#include "GY_KnobFinite.h"
#include <QDebug>
#include <QtMath>
#include <QPainter>
#include <QPen>
#include "Graphics/GY_GraphicsReflection.h"

GY_KnobFinite::GY_KnobFinite(const QString &guid, QGraphicsItem *parent) : GY_ContainerTransform(guid,this, parent)
{
    initializationPropertyInfo();
    m_pArc = new QGraphicsPathItem(this);
    this->setCenterPointF(m_PropertyInfoMap["Position"].propertyValue.toPointF());
    m_pTransFormGroup = new GY_TransformGroup(QString("%1_Group").arg(guid),this);
    m_ChildrensMap.insert(m_pTransFormGroup->getGuid(),m_pTransFormGroup);
    this->addToGroup(m_pTransFormGroup);
    updateDraw();
    setContainerEditPointF();
}

GY_KnobFinite::~GY_KnobFinite()
{

}

void GY_KnobFinite::setContainerEditPointF()
{
    QMap<QVariant,QPointF> editPoints = {
        {"RadiusPoint2",m_PropertyInfoMap["RadiusPoint2"].propertyValue.toPointF()},
        {"RadiusPoint1",m_PropertyInfoMap["RadiusPoint1"].propertyValue.toPointF()},
        {"ActiveAreaPoint1",m_PropertyInfoMap["ActiveAreaPoint1"].propertyValue.toPointF()},
        {"ActiveAreaPoint2",m_PropertyInfoMap["ActiveAreaPoint2"].propertyValue.toPointF()}
    };
    this->setEditPointF(editPoints);
}

GY_Object *GY_KnobFinite::copyItem(const QString &guid)
{
    if(getUserData(GY_ENUM::IsCopy) == 1) {
//        return nullptr;
        GY_Object *tmp;
        this->copyItemSig(&tmp);
        //更新copyItem属性列表
        QMap<QString,GY_PropertyInfo>::Iterator iterProperty = m_PropertyInfoMap.begin();
        while (iterProperty != m_PropertyInfoMap.end()) {
            if(((iterProperty.value().updateType)&PointOfRelative) == PointOfRelative) {
                tmp->updateProperty(iterProperty.key(), submittingPropertyValues(iterProperty.value().updateType,iterProperty.value().propertyValue,this));
            }else if(((iterProperty.value().updateType)&NotUpdate) == NotUpdate){
                ++iterProperty;
                continue;
            }else {
                tmp->updateProperty(iterProperty.key(), iterProperty.value().propertyValue);
            }
            ++iterProperty;
        }
        return tmp;
    }
    GY_Object* pObj;
    if(guid.isEmpty()) {
        pObj = (GY_Object*)GY_GraphicsReflection::getInstance()->newInstanceObjectClass(this->metaObject()->className(),this->getGuid()+"-"+QString::number(m_MyGuid));
        m_MyGuid++;
    }else {
        pObj = (GY_Object*)GY_GraphicsReflection::getInstance()->newInstanceObjectClass(this->metaObject()->className(),guid);
    }
    if(pObj == nullptr) {
        qDebug()<<this->getGuid()<<"isEmpty";
        return nullptr;
    }
    QVector<GY_Object *> childrens = m_pTransFormGroup->getOrderChildrens();
    int childrensSize = childrens.size();
    for(int i=0;i<childrensSize;i++) {
//        if(childrens[i]->copyItem() == nullptr) {
//            qDebug()<<childrens[i]->getGuid()<<"isEmpty";
//            continue;
//        }
        ((GY_ContainerTransform*)pObj)->addItem(childrens[i]->copyItem());
    }

    QMap<QString,GY_PropertyInfo>::Iterator iterProperty = m_PropertyInfoMap.begin();
    while (iterProperty != m_PropertyInfoMap.end()) {
        if(((iterProperty.value().updateType)&PointOfRelative) == PointOfRelative) {
            pObj->updateProperty(iterProperty.key(), submittingPropertyValues(iterProperty.value().updateType,iterProperty.value().propertyValue,this));
        }else if(((iterProperty.value().updateType)&NotUpdate) == NotUpdate){
            ++iterProperty;
            continue;
        }else {
            pObj->updateProperty(iterProperty.key(), iterProperty.value().propertyValue);
        }
        ++iterProperty;
    }
    connect(pObj,&GY_Object::copyItemSig,this,&GY_Object::originalVersion);
    pObj->setUserData(GY_ENUM::IsCopy, QVariant::fromValue(1));
    return pObj;
}

bool GY_KnobFinite::addItem(GY_Object *pItem)
{
    m_pTransFormGroup->addItem((GY_Object*)pItem);
    //添加item之后重新添加组，捕获编辑点更精确
    this->addToGroup(m_pTransFormGroup);
    updateDraw();
    return true;
}

bool GY_KnobFinite::removItem(const QString &guid)
{
    return m_pTransFormGroup->removItem(guid);
}

bool GY_KnobFinite::removItem(const GY_Object *pItem)
{
    bool remove = m_pTransFormGroup->removItem((GY_Object*)pItem);
    this->GY_ContainerTransform::removItem((GY_Object*)pItem);
    updateDraw();
    return remove;
}

void GY_KnobFinite::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QPen pen;
    //最后一位参数，颜色透明，默认运行状态
    pen.setColor(QColor(255,0,0,0));
    painter->setPen(pen);
    painter->drawPath(m_pArc->path());
    QGraphicsItemGroup::paint(painter,option,widget);
}

void GY_KnobFinite::initializationPropertyInfo()
{
    //隐藏
    GY_PropertyInfo isVisiblePropertyInfo(this);
    isVisiblePropertyInfo.propertyName="IsVisible";
    isVisiblePropertyInfo.propertyValue=true;
    isVisiblePropertyInfo.updateType = Update|OrdinaryAttributes;
    isVisiblePropertyInfo.propertyType = BoolPropertyType;
    isVisiblePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateIsVisible;
    m_PropertyInfoMap.insert(isVisiblePropertyInfo.propertyName,isVisiblePropertyInfo);
    m_PropertyInfoMap[isVisiblePropertyInfo.propertyName].setConnect();

    GY_PropertyInfo ObjectName(this);
    ObjectName.propertyName = "ObjectName";
    ObjectName.propertyValue = QString();
    ObjectName.updateType = Update|OrdinaryAttributes;
    ObjectName.propertyType = QStringPropertyType;
    ObjectName.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateObjectName;
    m_PropertyInfoMap.insert("ObjectName",ObjectName);
    m_PropertyInfoMap[ObjectName.propertyName].setConnect();

    //位置
    GY_PropertyInfo positonPropertyInfo(this);
    positonPropertyInfo.propertyName="Position";
    positonPropertyInfo.propertyValue=QPointF(0,0);
    positonPropertyInfo.updateType = Update|MoveUpdate|PointOfRelative;
    positonPropertyInfo.propertyType = PointPropertyType;
    positonPropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updatePosition;
    m_PropertyInfoMap.insert(positonPropertyInfo.propertyName,positonPropertyInfo);
    m_PropertyInfoMap[positonPropertyInfo.propertyName].setConnect();

    GY_PropertyInfo valuePropertyInfo(this);
    valuePropertyInfo.propertyName="CurrentValue";
    valuePropertyInfo.propertyValue=0;
    valuePropertyInfo.updateType = Update|OrdinaryAttributes;
    valuePropertyInfo.propertyType = QrealPropertyType;
    valuePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateValue;
    m_PropertyInfoMap.insert(valuePropertyInfo.propertyName,valuePropertyInfo);
    m_PropertyInfoMap[valuePropertyInfo.propertyName].setConnect();

    GY_PropertyInfo startValuePropertyInfo(this);
    startValuePropertyInfo.propertyName="StartValue";
    startValuePropertyInfo.propertyValue=0;
    startValuePropertyInfo.updateType = Update|OrdinaryAttributes;
    startValuePropertyInfo.propertyType = QrealPropertyType;
    startValuePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateStartValue;
    m_PropertyInfoMap.insert(startValuePropertyInfo.propertyName,startValuePropertyInfo);
    m_PropertyInfoMap[startValuePropertyInfo.propertyName].setConnect();

    GY_PropertyInfo EndValuePropertyInfo(this);
    EndValuePropertyInfo.propertyName="EndValue";
    EndValuePropertyInfo.propertyValue=1;
    EndValuePropertyInfo.updateType = Update|OrdinaryAttributes;
    EndValuePropertyInfo.propertyType = QrealPropertyType;
    EndValuePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateEndValue;
    m_PropertyInfoMap.insert(EndValuePropertyInfo.propertyName,EndValuePropertyInfo);
    m_PropertyInfoMap[EndValuePropertyInfo.propertyName].setConnect();

    GY_PropertyInfo StartAnglePropertyInfo(this);
    StartAnglePropertyInfo.propertyName="StartAngle";
    StartAnglePropertyInfo.propertyValue=0;
    StartAnglePropertyInfo.updateType = Update|OrdinaryAttributes;
    StartAnglePropertyInfo.propertyType = QrealPropertyType;
    StartAnglePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateStartAngle;
    m_PropertyInfoMap.insert(StartAnglePropertyInfo.propertyName,StartAnglePropertyInfo);
    m_PropertyInfoMap[StartAnglePropertyInfo.propertyName].setConnect();

    GY_PropertyInfo SweptAnglePropertyInfo(this);
    SweptAnglePropertyInfo.propertyName="SweptAngle";
    SweptAnglePropertyInfo.propertyValue=180;
    SweptAnglePropertyInfo.updateType = Update|OrdinaryAttributes;
    SweptAnglePropertyInfo.propertyType = QrealPropertyType;
    SweptAnglePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateSweptAngle;
    m_PropertyInfoMap.insert(SweptAnglePropertyInfo.propertyName,SweptAnglePropertyInfo);
    m_PropertyInfoMap[SweptAnglePropertyInfo.propertyName].setConnect();

    //半径点1
    GY_PropertyInfo RadiusPoint1PropertyInfo(this);
    RadiusPoint1PropertyInfo.propertyName="RadiusPoint1";
    RadiusPoint1PropertyInfo.propertyValue=QPointF(-50,0);
    RadiusPoint1PropertyInfo.updateType = NotUpdate;
    RadiusPoint1PropertyInfo.propertyType = PointPropertyType;
    RadiusPoint1PropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateRadiusPoint1;
    m_PropertyInfoMap.insert(RadiusPoint1PropertyInfo.propertyName,RadiusPoint1PropertyInfo);
    m_PropertyInfoMap[RadiusPoint1PropertyInfo.propertyName].setConnect();

    //半径点2
    GY_PropertyInfo RadiusPoint2PropertyInfo(this);
    RadiusPoint2PropertyInfo.propertyName="RadiusPoint2";
    RadiusPoint2PropertyInfo.propertyValue=QPointF(50,0);
    RadiusPoint2PropertyInfo.updateType = NotUpdate;
    RadiusPoint2PropertyInfo.propertyType = PointPropertyType;
    RadiusPoint2PropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateRadiusPoint2;
    m_PropertyInfoMap.insert(RadiusPoint2PropertyInfo.propertyName,RadiusPoint2PropertyInfo);
    m_PropertyInfoMap[RadiusPoint2PropertyInfo.propertyName].setConnect();

    //Arc半径点1
    GY_PropertyInfo ActiveAreaPoint1PropertyInfo(this);
    ActiveAreaPoint1PropertyInfo.propertyName="ActiveAreaPoint1";
    ActiveAreaPoint1PropertyInfo.propertyValue=QPointF(0,100);
    ActiveAreaPoint1PropertyInfo.updateType = NotUpdate;
    ActiveAreaPoint1PropertyInfo.propertyType = PointPropertyType;
    ActiveAreaPoint1PropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateActiveAreaPoint1;
    m_PropertyInfoMap.insert(ActiveAreaPoint1PropertyInfo.propertyName,ActiveAreaPoint1PropertyInfo);
    m_PropertyInfoMap[ActiveAreaPoint1PropertyInfo.propertyName].setConnect();

    //Arc半径点2
    GY_PropertyInfo ActiveAreaPoint2PropertyInfo(this);
    ActiveAreaPoint2PropertyInfo.propertyName="ActiveAreaPoint2";
    ActiveAreaPoint2PropertyInfo.propertyValue=QPointF(100,0);
    ActiveAreaPoint2PropertyInfo.updateType = NotUpdate;
    ActiveAreaPoint2PropertyInfo.propertyType = PointPropertyType;
    ActiveAreaPoint2PropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateActiveAreaPoint2;
    m_PropertyInfoMap.insert(ActiveAreaPoint2PropertyInfo.propertyName,ActiveAreaPoint2PropertyInfo);
    m_PropertyInfoMap[ActiveAreaPoint2PropertyInfo.propertyName].setConnect();

    GY_PropertyInfo ActiveAreaRadiusPropertyInfo(this);
    ActiveAreaRadiusPropertyInfo.propertyName="ActiveAreaRadius";
    ActiveAreaRadiusPropertyInfo.propertyValue=100;
    ActiveAreaRadiusPropertyInfo.updateType = Update|OrdinaryAttributes;
    ActiveAreaRadiusPropertyInfo.propertyType = QrealPropertyType;
    ActiveAreaRadiusPropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateActiveAreaRadius;
    m_PropertyInfoMap.insert(ActiveAreaRadiusPropertyInfo.propertyName,ActiveAreaRadiusPropertyInfo);
    m_PropertyInfoMap[ActiveAreaRadiusPropertyInfo.propertyName].setConnect();

    GY_PropertyInfo ActiveAreaStartAnglePropertyInfo(this);
    ActiveAreaStartAnglePropertyInfo.propertyName="ActiveAreaStartAngle";
    ActiveAreaStartAnglePropertyInfo.propertyValue=0;
    ActiveAreaStartAnglePropertyInfo.updateType = Update|OrdinaryAttributes;
    ActiveAreaStartAnglePropertyInfo.propertyType = QrealPropertyType;
    ActiveAreaStartAnglePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateActiveAreaStartAngle;
    m_PropertyInfoMap.insert(ActiveAreaStartAnglePropertyInfo.propertyName,ActiveAreaStartAnglePropertyInfo);
    m_PropertyInfoMap[ActiveAreaStartAnglePropertyInfo.propertyName].setConnect();

    GY_PropertyInfo ActiveAreaSweptAnglePropertyInfo(this);
    ActiveAreaSweptAnglePropertyInfo.propertyName="ActiveAreaSweptAngle";
    ActiveAreaSweptAnglePropertyInfo.propertyValue=90;
    ActiveAreaSweptAnglePropertyInfo.updateType = Update|OrdinaryAttributes;
    ActiveAreaSweptAnglePropertyInfo.propertyType = QrealPropertyType;
    ActiveAreaSweptAnglePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateActiveAreaSweptAngle;
    m_PropertyInfoMap.insert(ActiveAreaSweptAnglePropertyInfo.propertyName,ActiveAreaSweptAnglePropertyInfo);
    m_PropertyInfoMap[ActiveAreaSweptAnglePropertyInfo.propertyName].setConnect();

    GY_PropertyInfo ActiveAreaIncludeCenterPropertyInfo(this);
    ActiveAreaIncludeCenterPropertyInfo.propertyName="ActiveAreaIncludeCenter";
    ActiveAreaIncludeCenterPropertyInfo.propertyValue=true;
    ActiveAreaIncludeCenterPropertyInfo.updateType = Update|OrdinaryAttributes;
    ActiveAreaIncludeCenterPropertyInfo.propertyType = BoolPropertyType;
    ActiveAreaIncludeCenterPropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_KnobFinite::updateActiveAreaIncludeCenter;
    m_PropertyInfoMap.insert(ActiveAreaIncludeCenterPropertyInfo.propertyName,ActiveAreaIncludeCenterPropertyInfo);
    m_PropertyInfoMap[ActiveAreaIncludeCenterPropertyInfo.propertyName].setConnect();

}

bool GY_KnobFinite::updatePosition(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QPointF>())
    {
        return false;
    }
    //setpos设置为父级坐标
    QPointF point = value.toPointF();
    if(this->parentItem()) {
        point = this->mapToParent(this->mapFromScene(value.toPointF()));
    }
    this->setPos(point);
    m_PropertyInfoMap[name].propertyValue=this->mapFromScene(value.toPointF());
    updateDraw();
    calculateRadiusPoint2Position();
    calculateRadiusPoint1Position();
    return true;
}

bool GY_KnobFinite::updateValue(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value.toReal();
    updateDraw();
    return true;
}

bool GY_KnobFinite::updateStartValue(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value.toReal();
    updateDraw();
    return true;
}

bool GY_KnobFinite::updateEndValue(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value.toReal();
    updateDraw();
    return true;
}

bool GY_KnobFinite::updateStartAngle(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    if(value.toReal() > 180) {
        m_PropertyInfoMap[name].propertyValue=180;
    }else if(value.toReal() < -180) {
        m_PropertyInfoMap[name].propertyValue=-180;
    }else {
        m_PropertyInfoMap[name].propertyValue=value.toReal();
    }
    updateDraw();
    calculateRadiusPoint2Position();
    calculateRadiusPoint1Position();
    return true;
}

bool GY_KnobFinite::updateSweptAngle(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    if(value.toReal()>0) {
        m_direction = true;
    }else {
        m_direction = false;
    }
    if(value.toReal() > 360) {
        m_PropertyInfoMap[name].propertyValue=360;
    }else if(value.toReal() < -360) {
        m_PropertyInfoMap[name].propertyValue=-360;
    }else {
        m_PropertyInfoMap[name].propertyValue=value.toReal();
    }
    m_oldSweptAngle = m_PropertyInfoMap[name].propertyValue.toReal();
    updateDraw();
    calculateRadiusPoint1Position();
    return true;
}

void GY_KnobFinite::updateDraw()
{
    //设置旋转角度
    qreal startAngle = m_PropertyInfoMap["StartAngle"].propertyValue.toReal();
    qreal sweptAngle = m_PropertyInfoMap["SweptAngle"].propertyValue.toReal();
    qreal value = m_PropertyInfoMap["CurrentValue"].propertyValue.toReal();
    qreal startValue = m_PropertyInfoMap["StartValue"].propertyValue.toReal();
    qreal endValue = m_PropertyInfoMap["EndValue"].propertyValue.toReal();
    qreal proportion;
    if(startValue < endValue) {
        if(value <= startValue) {
            proportion = 0;
        }else if(value >= endValue) {
            proportion = 1;
        }else {
            qreal molecule = value - startValue;
            qreal denominator = endValue - startValue;
            proportion = molecule/denominator;
        }
    }else if(startValue > endValue) {
        if(value >= startValue) {
            proportion = 0;
        }else if(value <= endValue) {
            proportion = 1;
        }else {
            qreal molecule = value - endValue;
            qreal denominator = startValue - endValue;
            proportion = molecule/denominator;
            proportion = 1 - proportion;
        }
    }else {
       proportion = 0;
    }
    m_pTransFormGroup->setRotationCenter(QPointF(0,0));
    m_pTransFormGroup->setContainerRotation(startAngle+sweptAngle*proportion);

    //控制CurrentValue数值的扇形区域
    QPointF centerPoint= m_PropertyInfoMap.value("Position").propertyValue.toPointF();
    qreal activeAreaStartAngle=m_PropertyInfoMap.value("ActiveAreaStartAngle").propertyValue.toDouble();
    qreal activeAreaSweptAngle=m_PropertyInfoMap.value("ActiveAreaSweptAngle").propertyValue.toDouble();
    qreal Radius=m_PropertyInfoMap.value("ActiveAreaRadius").propertyValue.toDouble();
    QPointF innerDiffPoint=QPointF(centerPoint.x()-Radius,centerPoint.y()-Radius);
    QPainterPath Path;
    if(m_PropertyInfoMap["ActiveAreaIncludeCenter"].propertyValue.toBool() == true) {
        Path.moveTo(centerPoint);
        Path.arcTo(innerDiffPoint.x(),innerDiffPoint.y(),2*Radius,2*Radius,-activeAreaStartAngle,-((activeAreaSweptAngle)<0?(360+(activeAreaSweptAngle)):(activeAreaSweptAngle)));
        Path.closeSubpath();
        m_pArc->setPath(Path);
    }else {
        Path.arcMoveTo(innerDiffPoint.x(),innerDiffPoint.y(),2*Radius,2*Radius,-activeAreaStartAngle);
        Path.arcTo(innerDiffPoint.x(),innerDiffPoint.y(),2*Radius,2*Radius,-activeAreaStartAngle,-((activeAreaSweptAngle)<0?(360+(activeAreaSweptAngle)):(activeAreaSweptAngle)));
        Path.closeSubpath();
        m_pArc->setPath(Path);
    }
}

bool GY_KnobFinite::updateRadiusPoint2(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QPointF>())
    {
        return false;
    }
    QPointF point=this->mapFromScene(value.toPointF());
    //基于圆心的偏移坐标
    qreal pointAngle=precalculatedAngle(QPointF(0,0),point);
    if(pointAngle > 180) {
        m_PropertyInfoMap["StartAngle"].propertyValue=-(360-pointAngle);
    }else {
        m_PropertyInfoMap["StartAngle"].propertyValue=pointAngle;
    }
    calculateRadiusPoint1Position();
    QPointF Point1=precalculatedPosition(QPointF(0,0),50,m_PropertyInfoMap["StartAngle"].propertyValue.toDouble());//控制点位
    updateDraw();
    m_PropertyInfoMap[name].propertyValue=Point1;
    this->setEditPointF(name,Point1);
    return true;
}

//到360临界点会越过变成0.几;
bool GY_KnobFinite::updateRadiusPoint1(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QPointF>())
    {
        return false;
    }
    QPointF point=this->mapFromScene(value.toPointF());
    //基于圆心的偏移坐标
    qreal pointAngle=precalculatedAngle(QPointF(0,0),point);
    bool dir;
    if(pointAngle < m_oldPointAngle) {
        dir = false;//顺时针
    }else {
        dir = true;//逆时针
    }
    if((pointAngle - m_oldPointAngle)>180) {
        dir = false;
    }
    if((pointAngle - m_oldPointAngle)<-180) {
        dir = true;
    }
    if((m_PropertyInfoMap["SweptAngle"].propertyValue.toDouble() == 360)||(m_PropertyInfoMap["SweptAngle"].propertyValue.toDouble() == -360)) {

    }else {
        m_oldPointAngle = pointAngle;
    }
    if(!m_direction) {
        //负方向顺时针
        if(m_PropertyInfoMap["StartAngle"].propertyValue.toDouble() >= 0) {
            if(pointAngle <= m_PropertyInfoMap["StartAngle"].propertyValue.toDouble()) {
                m_PropertyInfoMap["SweptAngle"].propertyValue=pointAngle-(m_PropertyInfoMap["StartAngle"].propertyValue.toDouble());
            }else {
                m_PropertyInfoMap["SweptAngle"].propertyValue=pointAngle-m_PropertyInfoMap["StartAngle"].propertyValue.toDouble()-360;
            }
        }else {
            if(pointAngle <= m_PropertyInfoMap["StartAngle"].propertyValue.toDouble()+360) {
                m_PropertyInfoMap["SweptAngle"].propertyValue = pointAngle-(m_PropertyInfoMap["StartAngle"].propertyValue.toDouble()+360);
            }else {
                m_PropertyInfoMap["SweptAngle"].propertyValue = pointAngle-(m_PropertyInfoMap["StartAngle"].propertyValue.toDouble()+360)-360;
            }
        }
        if((m_oldSweptAngle <= m_PropertyInfoMap["SweptAngle"].propertyValue.toDouble())&&!dir) {
            m_PropertyInfoMap["SweptAngle"].propertyValue=-360;
        }else if((m_oldSweptAngle > m_PropertyInfoMap["SweptAngle"].propertyValue.toDouble())&&dir) {
            m_direction = true;
            m_PropertyInfoMap["SweptAngle"].propertyValue=0;
        }
    }else {
        if(m_PropertyInfoMap["StartAngle"].propertyValue.toDouble() >= 0) {
            if(pointAngle <= m_PropertyInfoMap["StartAngle"].propertyValue.toDouble()) {
                m_PropertyInfoMap["SweptAngle"].propertyValue=360-(m_PropertyInfoMap["StartAngle"].propertyValue.toDouble()-pointAngle);
            }else {
                m_PropertyInfoMap["SweptAngle"].propertyValue=pointAngle-m_PropertyInfoMap["StartAngle"].propertyValue.toDouble();
            }
        }else {
            if(pointAngle <= m_PropertyInfoMap["StartAngle"].propertyValue.toDouble()+360) {
                m_PropertyInfoMap["SweptAngle"].propertyValue = pointAngle+(-m_PropertyInfoMap["StartAngle"].propertyValue.toDouble());
            }else {
                m_PropertyInfoMap["SweptAngle"].propertyValue = pointAngle-(m_PropertyInfoMap["StartAngle"].propertyValue.toDouble()+360);
            }
        }
        if((m_oldSweptAngle >= m_PropertyInfoMap["SweptAngle"].propertyValue.toDouble())&&dir) {
            m_PropertyInfoMap["SweptAngle"].propertyValue=360;
        }else if((m_oldSweptAngle < m_PropertyInfoMap["SweptAngle"].propertyValue.toDouble())&&!dir) {
            m_direction = false;
            m_PropertyInfoMap["SweptAngle"].propertyValue=0;
        }
    }
    m_oldSweptAngle = m_PropertyInfoMap["SweptAngle"].propertyValue.toDouble();
    calculateRadiusPoint2Position();
    QPointF Point1=precalculatedPosition(QPointF(0,0),50,m_PropertyInfoMap["SweptAngle"].propertyValue.toDouble()+m_PropertyInfoMap["StartAngle"].propertyValue.toDouble());
    updateDraw();
    m_PropertyInfoMap[name].propertyValue=Point1;
    this->setEditPointF(name,Point1);
    return true;
}

bool GY_KnobFinite::updateActiveAreaPoint1(const QString &name, const QVariant &value)
{

}

bool GY_KnobFinite::updateActiveAreaPoint2(const QString &name, const QVariant &value)
{

}

bool GY_KnobFinite::updateIsVisible(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value;
    this->setVisible(value.toBool());
    return true;
}

qreal GY_KnobFinite::precalculatedAngle(const QPointF &centerPoint, const QPointF &calculatePoint)
{
    //基于圆心的偏移坐标
    QPointF diffPoint=calculatePoint-centerPoint;
    qreal pointAngle=qRadiansToDegrees(qAtan2(diffPoint.y(),diffPoint.x()));
    if(diffPoint.y()<0)
    {
        pointAngle+=360;
    }
    return pointAngle;
}

QPointF GY_KnobFinite::precalculatedPosition(const QPointF &centerPoint, const double radius, const double angele)
{
    qreal radian=qDegreesToRadians(angele); //弧度
    QPointF resultPoint;
    resultPoint.setX(cos(radian)*radius);
    resultPoint.setY(sin(radian)*radius);
    resultPoint+=centerPoint;
    return resultPoint;
}

void GY_KnobFinite::calculateRadiusPoint2Position()
{
    QPointF  RadiusPoint2=precalculatedPosition(QPointF(0,0),50,m_PropertyInfoMap["StartAngle"].propertyValue.toDouble());
    m_PropertyInfoMap["RadiusPoint2"].propertyValue=RadiusPoint2;
    this->setEditPointF("RadiusPoint2",RadiusPoint2);
}

void GY_KnobFinite::calculateRadiusPoint1Position()
{
    QPointF  RadiusPoint1=precalculatedPosition(QPointF(0,0),50,m_PropertyInfoMap["SweptAngle"].propertyValue.toDouble()+m_PropertyInfoMap["StartAngle"].propertyValue.toDouble());
    m_PropertyInfoMap["RadiusPoint1"].propertyValue=RadiusPoint1;
    this->setEditPointF("RadiusPoint1",RadiusPoint1);
    m_oldPointAngle = precalculatedAngle(QPointF(0,0),RadiusPoint1);
}

bool GY_KnobFinite::updateActiveAreaRadius(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value;
    updateDraw();
    return true;
}

bool GY_KnobFinite::updateActiveAreaStartAngle(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value;
    updateDraw();
    return true;
}

bool GY_KnobFinite::updateActiveAreaSweptAngle(const QString &name, const QVariant &value)
{
    if(!value.canConvert<qreal>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value;
    updateDraw();
    return true;
}

bool GY_KnobFinite::updateActiveAreaIncludeCenter(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value;
    updateDraw();
    return true;
}




